"""
Hello World 插件示例
"""
from datetime import datetime
from typing import Dict, Any, List
from fastapi import APIRouter

from plugin_system.base import BasePlugin, PluginInfo, PluginConfig


class HelloWorldPlugin(BasePlugin):
    """Hello World 插件"""
    
    def __init__(self):
        super().__init__()
        self.router = APIRouter(prefix="/hello", tags=["hello"])
        self._setup_routes()
    
    def get_info(self) -> PluginInfo:
        """获取插件信息"""
        return PluginInfo(
            name="hello_world",
            version="1.0.0",
            description="一个简单的Hello World插件示例",
            author="FastAPI Plugin System",
            homepage="https://github.com/fastapi-plugins",
            dependencies=[],
            tags=["example", "hello"],
            created_at=datetime.now(),
            updated_at=datetime.now()
        )
    
    def initialize(self) -> bool:
        """初始化插件"""
        try:
            self.info = self.get_info()
            self.config = PluginConfig(
                enabled=True,
                settings={"greeting": "Hello, World!"},
                auto_start=True
            )
            return True
        except Exception as e:
            print(f"Hello World插件初始化失败: {e}")
            return False
    
    def start(self) -> bool:
        """启动插件"""
        try:
            print("Hello World插件已启动")
            return True
        except Exception as e:
            print(f"Hello World插件启动失败: {e}")
            return False
    
    def stop(self) -> bool:
        """停止插件"""
        try:
            print("Hello World插件已停止")
            return True
        except Exception as e:
            print(f"Hello World插件停止失败: {e}")
            return False
    
    def get_routes(self) -> List[Dict[str, Any]]:
        """获取插件路由"""
        return [
            {
                "path": "/hello",
                "methods": ["GET"],
                "description": "获取Hello World消息"
            },
            {
                "path": "/hello/{name}",
                "methods": ["GET"],
                "description": "获取个性化Hello消息"
            }
        ]
    
    def _setup_routes(self):
        """设置路由"""
        
        @self.router.get("/")
        async def hello_world():
            """Hello World 端点"""
            greeting = self.config.settings.get("greeting", "Hello, World!")
            return {
                "message": greeting,
                "plugin": self.info.name,
                "version": self.info.version
            }
        
        @self.router.get("/{name}")
        async def hello_name(name: str):
            """个性化Hello端点"""
            return {
                "message": f"Hello, {name}!",
                "plugin": self.info.name,
                "version": self.info.version
            } 